package com.gamemost.dao;

import java.util.ArrayList;

import org.w3c.dom.Comment;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.gamemost.entity.DownloadedBook;

public class BookDatasource {
	public static String TB_ID = "_id";
	public static String BOOK_ID = "book_id";
	public static String BOOK_FILE_NAME = "file_name";
	public static String BOOK_IMAGE = "path_image";
	public static String BOOK_BOOKMARK = "bookmark";
	public static String CATEGORY_ID = "category_id";
	public static String TITLE = "title";
	public static String URL = "url";
	public static String BOOK_TYPE = "book_type";
	public static String TAG_BOOKCOVER = "tag_bookcover";

	private SQLiteDatabase database;
	private static MySQLiteHelper dbHelper;
	private static BookDatasource instance;

	public static BookDatasource getInstance(Context context) {
		if (instance == null) {
			instance = new BookDatasource(context);
		}
		return instance;
	}

	public BookDatasource(Context context) {
		System.out.println("init bookdatasource");
		database = MySQLiteHelper.getInstance(context).getWritableDatabase();
	}

	public void close() {
		dbHelper.close();
	}

	public void addBook(DownloadedBook book) {
		ContentValues values = new ContentValues();
		values.put(BOOK_ID, book.getId());
		values.put(BOOK_FILE_NAME, book.getFilename());
		values.put(BOOK_IMAGE, book.getImage());
		values.put(BOOK_BOOKMARK, "");
		values.put(CATEGORY_ID, book.getCategoryId());
		values.put(TITLE, book.getTitle());
		values.put(URL, book.getUrl());
		values.put(TAG_BOOKCOVER, book.getBookcover());
		values.put(BOOK_TYPE, String.valueOf(book.getTypeBook()));

		System.out.println("aaa info" + book.getId() + " " + book.getFilename()
				+ " " + book.getImage() + " type " + book.getTypeBook());
		long insertId = database.insert(MySQLiteHelper.DOWNLOAD_BOOK, null,
				values);
	}

	public ArrayList<DownloadedBook> getListBook(int from) {
		ArrayList<DownloadedBook> listBook = new ArrayList<DownloadedBook>();
		String[] allColumns = { BOOK_ID, BOOK_FILE_NAME, BOOK_IMAGE,
				BOOK_BOOKMARK, CATEGORY_ID,BOOK_TYPE };
		String sql = "select * from " + MySQLiteHelper.DOWNLOAD_BOOK;
		System.out.println("aaaaaabbbbbbbv");

		Cursor cursor = database.rawQuery(sql, null);
		cursor.moveToFirst();
		DownloadedBook aBook;
		String book_type = "",id = "", file_name = "", image = "", bookmark = "", category = "", title = "", url = "", bookcover = "";
		while (!cursor.isAfterLast()) {
			id = cursor.getString(1);
			file_name = cursor.getString(2);
			image = cursor.getString(3);
			bookmark = cursor.getString(4);
			category = cursor.getString(5);
			title = cursor.getString(6);
			url = cursor.getString(7);
			bookcover = cursor.getString(8);			
		book_type = cursor.getString(9);
			System.out.println(book_type+" aaaaaabbbbbbbccccccc '"+ bookmark+"'");

			aBook = new DownloadedBook(id, file_name, image, bookmark, Integer.parseInt(book_type));
			aBook.setCategoryId(category);
			aBook.setTitle(title);
			aBook.setUrl(url);
			aBook.setBookcover(bookcover);
			listBook.add(aBook);
			cursor.moveToNext();

		}
		cursor.close();
		return listBook;
	}

	public void updateDownloadedBook(DownloadedBook book) {
		ContentValues values = new ContentValues();
		System.out.println("bookmark update"+book.getBookmarkString());
		values.put(BOOK_BOOKMARK, book.getBookmarkString());
		database.update(MySQLiteHelper.DOWNLOAD_BOOK, values, BOOK_ID + "=?",
				new String[] { book.getId() });
	}
	public void updateTypeDownloadedBook(DownloadedBook book) {
		ContentValues values = new ContentValues();
		values.put(BOOK_TYPE, book.getTypeBook());
		database.update(MySQLiteHelper.DOWNLOAD_BOOK, values, BOOK_ID + "=?",
				new String[] { book.getId() });
	}
	public void deleteBook(DownloadedBook book) {
		database.delete(MySQLiteHelper.DOWNLOAD_BOOK, BOOK_ID + "=?",
				new String[] { book.getId() });
	}
	public void updateBookmarkReflow(DownloadedBook book) {
		System.out.println("updateBookmark start");
		ContentValues values = new ContentValues();
		System.out.println("updateBookmark "+book.getStringBMReflow());
		values.put(BOOK_BOOKMARK, book.getStringBMReflow());
		database.update(MySQLiteHelper.DOWNLOAD_BOOK, values, BOOK_ID + "=?",
				new String[] { book.getId() });
	}
}
